package com.tsd.config;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.tsd.core.cache.CacheConstants;
import com.tsd.core.cache.CacheOperatorApi;
import com.tsd.core.cache.utils.CreateRedisTemplateUtils;
import com.tsd.core.jwt.AccessToken;
import com.tsd.system.cache.*;
import com.tsd.system.entity.SysDataAuthExt;
import com.tsd.system.vo.GlobalSetting;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: CommonCacheConfig
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/11/18 11:34
 * @Version: 1.0
 */
@Configuration
@PropertySource(value = {"classpath:redis.properties"}, ignoreResourceNotFound = true)
public class CommonCacheConfig {

    @Value("${redis.config.enable}")
    private boolean enable = false;

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * 全局配置缓存
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "globalSettingCacheOperatorApi")
    public CacheOperatorApi<GlobalSetting> globalSettingCacheOperatorApi() {
        if (enable) {
            RedisTemplate<String, GlobalSetting> redisTemplate = CreateRedisTemplateUtils.createObject(redisConnectionFactory);
            return new GlobalSettingRedisCache(redisTemplate);
        }
        TimedCache<String, GlobalSetting> timedCache = CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT_HOUR);
        return new GlobalSettingCache(timedCache);
    }

    /**
     * 用户数据授权缓存
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "sysDataAuthCacheOperatorApi")
    public CacheOperatorApi<List<SysDataAuthExt>> sysDataAuthCacheOperatorApi() {
        if (enable) {
            RedisTemplate<String, List<SysDataAuthExt>> redisTemplate = CreateRedisTemplateUtils.createObject(redisConnectionFactory);
            return new SysDataAuthRedisCache(redisTemplate);
        }
        TimedCache<String, List<SysDataAuthExt>> timedCache = CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT_HOUR);
        return new SysDataAuthCache(timedCache);
    }

    /**
     * Token缓存
     * 用于限制一个账户不能同时登录多个设备
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "tokenCacheOperatorApi")
    public CacheOperatorApi<AccessToken> tokenCacheOperatorApi() {
        if (enable) {
            RedisTemplate<String, AccessToken> redisTemplate = CreateRedisTemplateUtils.createObject(redisConnectionFactory);
            return new TokenRedisCache(redisTemplate);
        }
        TimedCache<String, AccessToken> timedCache = CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT_HOUR);
        return new TokenCache(timedCache);
    }
}
